package boot

import (
	"fmt"
	"github.com/go-playground/validator/v10"
	"github.com/iris-contrib/swagger/v12"
	"github.com/iris-contrib/swagger/v12/swaggerFiles"
	"github.com/kataras/iris/v12"
	"wit-admin-go/business/model"
	"wit-admin-go/conf"
	"wit-admin-go/system/middleware"
	"wit-admin-go/system/router"
	"wit-admin-go/system/utils"
)

func WitApp() *iris.Application {
	app := utils.Wit
	app.Configure(iris.WithOptimizations)
	app.AllowMethods(iris.MethodOptions)
	// 初始化数据库
	//utils.InitDB()
	model.InitBusinessDB()
	// 初始化Redis
	if conf.CONFIG.WitAdmin.Redis {
		utils.RedisDB()
	}
	// 加载日志配置
	WitLog(app)
	// 路由全局中间件
	app.UseGlobal(middleware.Before)
	app.DoneGlobal(middleware.After)
	// 加载路由
	router.RegisterRouter(app)
	// webSocket
	utils.InitWebSocket(app)
	// 模型绑定和验证
	app.Validator = validator.New()
	// swagger配置
	config := &swagger.Config{
		URL: conf.CONFIG.WitAdmin.SwaggerDocUrl + ":" + conf.CONFIG.WitAdmin.Port + "/wit/wit-doc.json",
	}
	app.Get("/wit/{any:path}", swagger.CustomWrapHandler(config,
		swaggerFiles.Handler))
	utils.Wit = app
	fmt.Println("================================================\n" +
		"*                                    tt        *\n" +
		"*     ww      ww      ww    oo     ttttttt     *\n" +
		"*       ww  ww  ww  ww      ii       tt        *\n" +
		"*         ww      ww        ii       tttt      *\n" +
		"================================================")
	return app
}
